﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace cms.libs
{
    [Serializable]
    public class DBIncome
    {
        public int CountAcc { get; set; }
        public int CountBet { get; set; }
        public int SUMBetMoney { get; set; }
        public int SUMWinMoney { get; set; }
        public int SUMProfit { get; set; }
        public int CountAccNew { get; set; }
        public int CountBetNew { get; set; }
        public DateTime DateWin { get; set; }


        public List<DBIncome> GetList(DateTime fromdate, DateTime todate)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                var oCommand = new SqlCommand("cms_SPReport_Revenue");
                oCommand.CommandType = CommandType.StoredProcedure;
                List<DBIncome> lRet = db.GetList<DBIncome>(oCommand,
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new List<DBIncome>();
            }
        }

        public DataTable GetListDataTable(DateTime fromdate, DateTime todate)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                var oCommand = new SqlCommand("cms_SPReport_Revenue");
                oCommand.CommandType = CommandType.StoredProcedure;
                DataTable lRet = db.getDataTableSP("cms_SPReport_Revenue    ",
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }
    }

    [Serializable]
    public class DBRoomReport
    {
        public int CountBet { get; set; }
        public int CountAccount { get; set; }
        public int RoomID { get; set; }
        public int SUMBetMoney { get; set; }
        public int SUMWinMoney { get; set; }
        public int SUMProfitMoney { get; set; }

        public List<DBRoomReport> GetList(DateTime fromdate, DateTime todate)
        {
            DBHelper db = null;
            db = new DBHelper(Config.SQLConn);
            var oCommand = new SqlCommand("cms_SPReportRoomInfo");
            oCommand.CommandType = CommandType.StoredProcedure;
            List<DBRoomReport> lRet = db.GetList<DBRoomReport>(oCommand,
                                                   new SqlParameter("@FromDate", fromdate),
                                                   new SqlParameter("@ToDate", todate)
                );


            return lRet;
        }

        public DataTable GetListDataTable(DateTime fromdate, DateTime todate, int roomid)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportRoomInfo",
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate),
                                                           new SqlParameter("@RoomID", roomid)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }
    }

    [Serializable]
    public class DBAccountReport
    {
        public DataTable GetListDataTable(string AccName,DateTime fromdate, DateTime todate)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportAccountInfo",
                                                            new SqlParameter("@AccountName", AccName),
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetListByAccountId(DateTime fromdate, DateTime todate, int accId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportAccountInfo_byAccID",
                                                            new SqlParameter("@AccountID", accId),
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetListByACcIdAndRoomId(int dateReport, int accId, int RoomId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportAccountInfo_byAccID_InRoomID",
                                                            new SqlParameter("@AccountID", accId),
                                                            new SqlParameter("@RoomID", RoomId),
                                                           new SqlParameter("@DateReport", dateReport)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetListAccountJackpot(DateTime fromdate, DateTime todate, string accName, int RoomId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPGetHistoryJackport",
                                                            new SqlParameter("@AccountName ", accName),
                                                            new SqlParameter("@RoomID", RoomId),
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetListAccountTopBet(DateTime fromdate, DateTime todate, int roomId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPGetTopBetAccount", new SqlParameter("@RoomID", roomId),
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetListAccountTopWin(DateTime fromdate, DateTime todate, int RoomId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPGetTopWinAccount",
                    new SqlParameter("@RoomID ", RoomId),
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public DataTable GetBetLogSpin(int betlogId)
        {
            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportBetLogInfo",
                                                           new SqlParameter("@BetLogID ", betlogId)
                    );


                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public void GetInfoWithResult(long betlogId, ref string jsondetail, ref string result, ref int roomid,ref string betline, ref string bonnusGame)
        {
            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                var output1 = new SqlParameter("@JsonWinDetail", SqlDbType.VarChar, 2000);
                output1.Direction = ParameterDirection.Output;

                var output2 = new SqlParameter("@ResultString", SqlDbType.VarChar, 50);
                output2.Direction = ParameterDirection.Output;

                var output3 = new SqlParameter("@RoomID", SqlDbType.Int);
                output3.Direction = ParameterDirection.Output;

                var output4 = new SqlParameter("@BetLine", SqlDbType.NVarChar, 50);
                output4.Direction = ParameterDirection.Output;

                var output5 = new SqlParameter("@BonnusMiniGame", SqlDbType.NVarChar, 50);
                output5.Direction = ParameterDirection.Output;

                db.ExecuteScalarSP("cms_SPReportBetLogDetailInfo", new SqlParameter("@BetLogID", betlogId), output1, output2, output3, output4, output5);

                jsondetail = output1.Value.ToString();
                result = output2.Value.ToString();
                roomid = int.Parse(output3.Value.ToString());
                betline = output4.Value.ToString();
                bonnusGame = output5.Value.ToString();
            }
            catch (Exception ex)
            {

            }
        }

        public void GetFundMoney(int roomid, ref long PrizeFund, ref int Jackpot, ref long Profit)
        {
            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                var output1 = new SqlParameter("@PrizeFund", SqlDbType.BigInt);
                output1.Direction = ParameterDirection.Output;
                var output2 = new SqlParameter("@Jackpot", SqlDbType.Int);
                output2.Direction = ParameterDirection.Output;
                var output3 = new SqlParameter("@Profit", SqlDbType.BigInt);
                output3.Direction = ParameterDirection.Output;

                db.ExecuteScalarSP("cmsSP_GetCurrentFundMoney", new SqlParameter("@RoomID", roomid), output1, output2, output3);

                PrizeFund = long.Parse(output1.Value.ToString());
                Jackpot = int.Parse(output2.Value.ToString());
                Profit = long.Parse(output3.Value.ToString());

            }
            catch (Exception ex)
            {

            }
        }
        public DataTable ReportRoomAccount(DateTime fromdate, DateTime todate, int RoomId)
        {
            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cms_SPReportRoomAccount",
                                                           new SqlParameter("@FromDate", fromdate),
                                                           new SqlParameter("@ToDate", todate),
                                                           new SqlParameter("@RoomID", RoomId)
                    );

                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }
    }

    [Serializable]
    public class DBUpdateInfo
    {
        public DataTable GetCurrentProfit()
        {

            try
            {
                DBHelper db = null;
                db = new DBHelper(Config.SQLConn);
                DataTable lRet = db.getDataTableSP("cmsSP_GetCurrentProfitPer");

                return lRet;
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        public int UpdateProfit(int roomid, int profit)
        {
            int responsestatus = -1;

            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                var output1 = new SqlParameter("@Response", SqlDbType.Int);
                output1.Direction = ParameterDirection.Output;

                db.ExecuteScalarSP("UpdateFundMoney_ProfitPer", new SqlParameter("@RoomID ", roomid),
                    new SqlParameter("@ProfitMoneyPer ", profit),
                    output1
                    );

                responsestatus = int.Parse(output1.Value.ToString());

                return responsestatus;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int UpdateJackpot(int roomid, int jackpot)
        {
            int responsestatus = -1;

            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                var output1 = new SqlParameter("@Response", SqlDbType.Int);
                output1.Direction = ParameterDirection.Output;

                db.ExecuteScalarSP("UpdateFundMoney_JackpotPer", new SqlParameter("@RoomID ", roomid),
                    new SqlParameter("@JackpotPer ", jackpot),
                    output1
                    );

                responsestatus = int.Parse(output1.Value.ToString());

                return responsestatus;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }

    [Serializable]
    public class DBUserInfo
    {
        public int UpdatePass(int userid, string oldpass, string newpass)
        {

            try
            {
                DBHelper db = null;

                db = new DBHelper(Config.SQLConn);
                var output1 = new SqlParameter("@Response", SqlDbType.Int);
                output1.Direction = ParameterDirection.Output;

                db.ExecuteScalarSP("cms_ChangePass", new SqlParameter("@UserID ", userid),
                    new SqlParameter("@NewPassWord ", newpass),
                    new SqlParameter("@OldPassWord ", oldpass),
                    output1
                    );

                int responsestatus = int.Parse(output1.Value.ToString());

                return responsestatus;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
